package controller;

import model.FileRules;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description: 文件控制类
 * author: 时间
 * time: 2022-01-09-15:05
 */

public class OperationFile {
    //测试文件读取方法
    @Test
    public void test01(){
        OperationFile o1 = new OperationFile();
        String path = "D:\\code\\workspace_idea\\bbfen\\myFirstProject\\src\\model\\registered.txt";


        StringBuffer result = o1.readFile(path);
        System.out.println(result);
    }

    //测试文件写入方法
    @Test
    public void test02(){
        OperationFile o1 = new OperationFile();
        String path = "D:\\code\\workspace_idea\\bbfen\\myFirstProject\\src\\model\\registered.txt";
        String string = "2020203361-qq1507298999";

        o1.writeFile(string,path);

    }

    //测试过滤特定信息的方法，例：过滤阶梯教室
    @Test
    public void test03() throws IOException {

        OperationFile o2 = new OperationFile();



        StringBuffer stringBuffer;
        stringBuffer =  o2.readTheater();

        System.out.println(stringBuffer);
    }
    /**
     * Description: 读取给定路径的文件，返回一个StringBuffer
     * Author: Iris
     * Date: 2022/1/9 18:00
     */
    public StringBuffer readFile(String path){
        StringBuffer stringBuffer = new StringBuffer();
        String str;
        File file = new File(path);
        FileReader f = null;
        BufferedReader f1 = null;
        try {
            f = new FileReader(file);
            f1 = new BufferedReader(f);
            while ((str = f1.readLine()) != null){
                stringBuffer.append(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                assert f1 != null;
                f1.close();
                f.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer;
    }


    /**
     * description: 将指定字符串写入到文件中，参数String为要写入的字符串，参数path为要写入文件的地址
     * author: Iris
     * time: 2022-01-09-15:05
     */
    public void writeFile(String string,String path){
        File file = new File(path);
        FileWriter f = null;
        BufferedWriter f1 = null;
        try {
            f = new FileWriter(file, true);   // 追加写入
            f1 = new BufferedWriter(f);
            f1.newLine();   // 换行
            f1.write("(" +string + ")");   // 写入
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                assert f1 != null;
                f1.close();
                f.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * Description: 读账号密码文件，格式为：(201920321243-qq1507298022)
     * Author: Iris
     * Date: 2022/1/9 18:00
     */
    public StringBuffer readRegisteredFile(){
        OperationFile o1 = new OperationFile();
        String path = FileRules.pathRe;

        StringBuffer stringBuffer = o1.readFile(path);
        return stringBuffer;
    }

    /**
     * Description: 向文件中写入账号密码，格式：(201920321243-qq1507298022)
     * Author: Iris
     * Date: 2022/1/10 11:22
     */
    public void writeRegisteredFile(String string){
        OperationFile o1 = new OperationFile();
        String path = "src\\model\\registered.txt";

        o1.writeFile(string,path);

    }
    /**
     * Description: 更改文件中指定账号的密码
     * Author: Iris
     * Date: 2022/1/11 11:16
     */
    //如果str中账号等于给定账号，删除该条数据，新加一条数据
    public void changeRegisteredFile(String account, String password) throws IOException {

        String path = "src\\model\\registered.txt";
        //1.删除掉原账号密码
        OperationFile1 o1 = new OperationFile1();
        o1.deleteString(account,0, path);

        //2.写入新的账号密码
        OperationFile o2 = new OperationFile();

        String string = account + "-" + password;

        o2.writeFile(string,path);
    }



    /**
     * Description: 读取教室信息，格式：(A101-智慧教室-30~40-空闲-无)
     * Author: 时间
     * Date: 2022/1/11 16:36
     */
    public StringBuffer readClassroomFile(){
        OperationFile o1 = new OperationFile();
        String path = "src\\model\\classroom.txt";

        return o1.readFile(path);
    }

    /**
     * Description: 写入教室信息,格式：(A101-智慧教室-30~40-空闲-无)
     * Author: Iris
     * Date: 2022/1/11 16:37
     */
    public void writeClassroomFile(String string){
        OperationFile o1 = new OperationFile();
        String path = "src\\model\\classroom.txt";

        o1.writeFile(string,path);
    }

    /**
     * Description: 更改教室信息，参数（教室号，整个教室信息），将指定教室号的信息改为传进来的信息(A101-智慧教室-30~40-空闲-无)
     * Author: Iris
     * Date: 2022/1/13 16:37
     */
    public void changeClassroomFile(String number, String information) throws IOException {

        String path = "src\\model\\classroom.txt";
        //1.删除掉原教室信息
        OperationFile1 o1 = new OperationFile1();
        o1.deleteString(number,0, path);

        //2.写入新的教室信息
        OperationFile o2 = new OperationFile();
        o2.writeFile(information,path);
    }

    /**
     * Description: 删除指定教室信息,参数：教室号
     *              如果匹配出的教室号和给定教室号相等则删除
     * Author: Iris
     * Date: 2022/1/13 16:00
     */
    public void deleteClassroomFile(String string, String pathCl) throws IOException {

        String path = FileRules.pathCl;
        OperationFile1 o1 = new OperationFile1();
        o1.deleteString(string,0, path);
    }

//    /**
//     * Description: 筛选出学生和教师中已注册用户，返回该用户的信息，存入数组
//     * Author: 时间
//     * Date: 2022/1/12 14:45
//     */
//    //这里是想实现，给定一个账号，然后能返回该账号信息的意思吗
//    public String[] compareUserAndReg(){
//
//        return new String[]{};
//    }

    /**
     * Description: 读已注册的学生信息
     * Author: 时间
     * Date: 2022/1/12 18:03
     */
    //读入信息时，判断 str中-前边的部分长度是几位数，是学生的长度则加入
    public StringBuffer readStudentFile(){
        OperationFile o1 = new OperationFile();
        String path = FileRules.pathSt;

        return o1.readFile(path);
    }

    /**
     * Description: 读已注册的教师信息
     * Author: Iris
     * Date: 2022/1/12 18:05
     */
    //读入信息时，判断 str中-前边的部分长度是几位数，是教师的长度则加入
    public StringBuffer readTeacherFile(){
        OperationFile o1 = new OperationFile();
        String path = FileRules.pathTe;

        return o1.readFile(path);
    }

    /**
     * Description: 只读取出普通教室的信息
     * Author: Iris
     * Date: 2022/1/12 18:05
     */
    public StringBuffer readCommonClassroom() throws IOException {

        OperationFile1 o1 = new OperationFile1();
        StringBuffer stringBuffer = o1.getDeterminedString("普通教室",1, FileRules.pathCl);

        return  stringBuffer;
    }

    /**
     * Description: 只读取出普通教室的信息
     * Author: Iris
     * Date: 2022/1/12 18:05
     */
    public StringBuffer readsmartClassroom() throws IOException {

        OperationFile1 o1 = new OperationFile1();
        StringBuffer stringBuffer = o1.getDeterminedString("智慧教室",1, FileRules.pathCl);

        return  stringBuffer;
    }

    /**
     * Description: 只读取出阶梯教室的信息
     * Author: Iris
     * Date: 2022/1/12 18:05
     */
    public StringBuffer readTheater() throws IOException {

        OperationFile1 o1 = new OperationFile1();
        StringBuffer stringBuffer = o1.getDeterminedString("阶梯教室",1, FileRules.pathCl);

        return  stringBuffer;
    }

    /**
     * Description: 写入一条历史预约信息,格式：（学号/工号-教室号-教室类型-开始时间-结束时间-手机号-备注）
     * @param string 这个参数表示
     * Author: Iris
     * Date: 2022/1/11 16:37
     */
    public void write(String string){

        OperationFile o1 = new OperationFile();
        String path = FileRules.pathOr;

        o1.writeFile(string,path);
    }

    /**
     *Description: 预约一个教室，把这个预约的教室写入已预约教室信息表中,格式格式（学号/工号-教室号-教室类型-开始时间-结束时间-手机号-备注）
     * Author: Iris
     *@param str1 这个参数表示用户预约的教室号
     *@param str2 这个参数表示用户写入的其它信息，格式：开始时间-结束时间-手机号-备注，例：10：00-12：00-18220818025-开会使用
     */
    public void orderClassroom(String str1,String str2) throws IOException {

        OperationFile o1 = new OperationFile();
        OperationFile1 o2 = new OperationFile1();
        String path = FileRules.pathOr;

        //获取指定教室的教室类型
        String str = o2.getDeterminedString(str1,0,path).toString();
        String tempString = str.substring(1,str.length()-1);
        String[] users = tempString.split("-");
        String type = users[1];

        //拼接要写入的信息
        String string = "(" + str1 + type + str2 +")";
        o1.writeFile(string,path);

    }



    /**
     * Description: 取消预约一个教室，把一条历史预约信息从已预约教室信息表中删除
     * Author: Iris
     * @param string 这个参数表示用户的学号
     * Date: 2022/1/14 18:05
     */
    public void deleteOrderClassroom(String string) throws IOException {

        OperationFile1 o1 = new OperationFile1();

        o1.deleteString(string,1,FileRules.pathOr);
    }


    /**
     * Description: 修改预约一个教室
     * Author: Iris
     * @param str1 这个参数表示用户的学号
     * @param str1 这个参数表示用户的学号
     * Date: 2022/1/14 18:05
     */
    public void updateOrderClassroom(String str1,String str2) throws IOException {

        //1.删除掉原教室信息
        OperationFile1 o1 = new OperationFile1();
        o1.deleteString(str1,0,FileRules.pathCl);

        //2.写入新的教室信息
        OperationFile o2 = new OperationFile();
        String path = FileRules.pathCl;
        String string = str2 + "";

        o2.writeFile(string,path);
    }

    /**
     * Description: 查看教室预约信息
     * Author: Iris
     * Date: 2022/1/14 18:05
     */
    public StringBuffer checkOrderClassroom(){

        OperationFile o1 = new OperationFile();
        String path = FileRules.pathOr ;

        StringBuffer stringBuffer = o1.readFile(path);
        return stringBuffer;
    }

    /**
     * Description: 读取已预约信息
     * Author: 时间
     * Date: 2022/1/14 22:09
     * @return
     */
    public ArrayList<StringBuffer> readOrderInformation(String count, Pattern pattern){//如果该账号已预约多条信息呢？//返回StringBuffer数组吧
        StringBuffer str1 = checkOrderClassroom();
        ArrayList<StringBuffer> result = new ArrayList<>();
        int i =0;
        Matcher matcher = pattern.matcher(str1);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-)
            // 字符串截取，去掉第一个与最后一个
            String str2 = str.substring(1, str.length() - 1);   // 201920321243
            String[] strings = str2.split("-");
            if (count.equals(strings[0])){
//                return new StringBuffer(str2);
                result.add(new StringBuffer(str2));

            }
        }
        return result;
    }

    /**
     * Description: 读当前登录的学生或教师信息
     * Author: 时间
     * Date: 2022/1/14 22:10
     */
    public StringBuffer readStuAndTea(String count, Pattern pattern){
        StringBuffer str1 = readStudentFile();
        StringBuffer str2 = readTeacherFile();
        Matcher matcher1 = pattern.matcher(str1);
        Matcher matcher2 = pattern.matcher(str2);
        while (matcher1.find()) {
            String str = matcher1.group(0);  // (201920321243-)
            // 字符串截取，去掉第一个与最后一个
            String str3 = str.substring(1, str.length() - 1);   // 201920321243
            String[] strings = str3.split("-");
            if (count.equals(strings[3])){
                return new StringBuffer(str3);
            }
        }
        while (matcher2.find()) {
            String str = matcher2.group(0);  // (201920321243-)
            // 字符串截取，去掉第一个与最后一个
            String str3 = str.substring(1, str.length() - 1);   // 201920321243
            String[] strings = str3.split("-");
            if (count.equals(strings[3])){
                return new StringBuffer(str3);
            }
        }
        return null;
    }
}
